package com.onlyxiahui.framework.action.dispatcher.common;

import java.lang.reflect.Method;

import org.springframework.core.GenericTypeResolver;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.SynthesizingMethodParameter;

/**
 * Date 2019-01-11 22:32:32<br>
 * Description
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */

public class ActionMethod {

	private Class<?> actionClass;
	private Method method;
	private MethodParameter[] methodParameters;
	private String methodPath;

//	public ActionMethod(Class<?> actionClass, Method method) {
//		this.actionClass = actionClass;
//		this.method = method;
//		initialize(method);
//	}

	public ActionMethod(Method method, String methodPath) {
		this.actionClass = method.getDeclaringClass();
		this.method = method;
		this.methodPath = methodPath;
		initialize(method);
	}

	private void initialize(Method method) {
		methodParameters = initMethodParameters(method);
	}

	private MethodParameter[] initMethodParameters(Method method) {
		int count = method.getParameterCount();
		MethodParameter[] result = new MethodParameter[count];
		for (int i = 0; i < count; i++) {
			SynthesizingMethodParameter parameter = SynthesizingMethodParameter.forExecutable(method, i);
			GenericTypeResolver.resolveParameterType(parameter, this.actionClass);
			result[i] = parameter;
		}
		return result;
	}

	public Class<?> getActionClass() {
		return actionClass;
	}

	public Method getMethod() {
		return method;
	}

	public MethodParameter[] getMethodParameters() {
		return methodParameters;
	}

	public String getMethodPath() {
		return methodPath;
	}

	public void setMethodPath(String methodPath) {
		this.methodPath = methodPath;
	}
}
